package com.glory.wxmemsicpda.bean.net;

import androidx.fragment.app.FragmentManager;

import com.glory.wxmemsicpda.base.App;
import com.google.gson.annotations.SerializedName;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import java.io.Serializable;
import java.util.ArrayList;


public class BaseResponseBean<T> implements Serializable {

    private ResponseBean<T> Response;

    private transient FreeUI_GeneralFragmentDialog tipDialog;

    public ResponseBean<T> getResponse() {
        return Response;
    }

    public void setResponse(ResponseBean<T> response) {
        Response = response;
    }


    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(boolean isHaveBody, FragmentManager fragmentManager) {
        if (Response.getHeader().getRESULT().toUpperCase().equals("SUCCESS")) {
            if (isHaveBody) {
                if (Response.getBody() == null) {
                    //FreeApi_ToastUtils.showFreeToast("暂无请求内容", context, false, Toast.LENGTH_SHORT);
                    showFailDialog("暂无请求内容", fragmentManager);
                    return false;
                } else {
                    if (Response.getBody() instanceof ArrayList) {
                        if (((ArrayList) Response.getBody()).size() > 0) {
                            return true;
                        } else {
                            //FreeApi_ToastUtils.showFreeToast("暂无请求数据", context, false, Toast.LENGTH_SHORT);
                            showFailDialog("暂无请求数据", fragmentManager);
                            return false;
                        }
                    } else {
                        return true;
                    }
                }
            } else {
                return true;
            }
        } else {
            showFailDialog(Response.getHeader().getRESULTMESSAGE(), fragmentManager);
            //FreeApi_ToastUtils.showFreeToast(Response.getReturn().getReturnMessage(), context, false, Toast.LENGTH_SHORT);
            return false;
        }
    }


    private void showFailDialog(String message, FragmentManager fragmentManager) {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.Companion.getApp(), "确定", message, new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {

            }
        });
        tipDialog.show(fragmentManager, "DIALOG");
    }
}
